package com.hongning.railway.ui.main.fragments;

import android.app.Application;
import android.arch.lifecycle.ViewModel;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.Constant;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.LoginEntity;
import com.hongning.railway.entity.MineItemBean;
import com.hongning.railway.entity.UserEntity;
import com.hongning.railway.entity.reqbean.ReqScoreBean;
import com.hongning.railway.entity.resbean.ResScoreRankInfo;
import com.hongning.railway.ui.login.LoginActivity;
import com.hongning.railway.ui.main.fragments.cadremanagement.CadreManagementFragment;
import com.hongning.railway.ui.main.fragments.home.itemviewmodel.MineItemViewModel;
import com.hongning.railway.ui.main.fragments.threesessionsandonelesson.ThreesessionSanDoneLessonViewModel;
import com.hongning.railway.ui.main.mine.PartyMemberInfoActivity;
import com.hongning.railway.util.Apputils;
import com.hongning.railway.util.GsonUtil;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class MineViewModel extends BaseViewModel<AppRepository> {

    public ObservableList<MineItemViewModel> mineItemViewModels = new ObservableArrayList<>();
    public ItemBinding<MineItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.mine_item);
    public ObservableField<String> content = new ObservableField<>("个人中心");
    public ObservableField<LoginEntity> loginEntity = new ObservableField<>(Apputils.getLoginInfo());

    public ObservableField<String> allscore = new ObservableField<>();
    public ObservableField<String> hnscore = new ObservableField<>();
    public ObservableField<String> learnscore = new ObservableField<>();
    private ResScoreRankInfo resScoreRankInfo;
    //按钮的点击事件
    public BindingCommand onClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (Apputils.getLoginInfo() == null) {
                ToastUtils.showShort("请先登录");
                startActivity(LoginActivity.class);
                return;
            }

           startContainerActivity(PartyMemberInfoActivity.class.getName());
        }
    });


    public MineViewModel(@NonNull Application application,AppRepository appRepository) {
        super(application,appRepository);
        List<MineItemViewModel> datas = new ArrayList<>();
        datas.add(new MineItemViewModel(this, new MineItemBean("我的消息", "wdxx", R.mipmap.mine_wdxx_icon, true)));
        datas.add(new MineItemViewModel(this, new MineItemBean("积分排行", "jfph", R.mipmap.mine_jfpm_icon, false)));
        datas.add(new MineItemViewModel(this, new MineItemBean("三会一课", "shyk", R.mipmap.mine_shyk_icon, false)));
        datas.add(new MineItemViewModel(this, new MineItemBean("干部管理", "gbgl", R.mipmap.mine_gbgl_icon, false)));
        datas.add(new MineItemViewModel(this, new MineItemBean("干部测评", "gbcp", R.mipmap.mine_gbpx_icon, false)));
        datas.add(new MineItemViewModel(this, new MineItemBean("党建考核", "djkh", R.mipmap.mine_gyapp_icon, false)));
        datas.add(new MineItemViewModel(this, new MineItemBean("党员测评", "dycp", R.mipmap.mine_gyapp_icon, false)));
        datas.add(new MineItemViewModel(this, new MineItemBean("心得体会", "xdth", R.mipmap.mine_gyapp_icon, false)));
        datas.add(new MineItemViewModel(this, new MineItemBean("关于APP", "gyapp", R.mipmap.mine_gyapp_icon, false)));
        mineItemViewModels.addAll(datas);
    }

    public void loadScoreInfo() {
        ReqScoreBean reqScoreBean = new ReqScoreBean();
        reqScoreBean.setStaffId(Apputils.getStaffid());
        model.scoreRank(reqScoreBean)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .subscribe(new ApiDisposableObserver<ResScoreRankInfo>() {
                    @Override
                    public void onResult(ResScoreRankInfo resScoreRankInfo) {
                        MineViewModel.this.resScoreRankInfo = resScoreRankInfo;
                        learnscore.set(resScoreRankInfo.getStrongCountryScore()+"");
                        hnscore.set(resScoreRankInfo.getHNScore());
                        allscore.set(resScoreRankInfo.getSumScore());
                    }
                });
    }

    public ResScoreRankInfo getScoreInfo(){
        if (resScoreRankInfo!=null)return resScoreRankInfo;
        return null;
    }
}
